/**
 * @author Stephan Pfaff (http://www.derdaumen.net)
 */

/**
 * The Category Object
 * @param vars object providing the category properties (loaded from the database)
 * @return
 */

function Category (vars){
	if (vars != null) {
		try {
			Item.call(this, vars.prefix, vars.id, vars.type, vars.basename, vars.path, vars.label, vars.alias, vars.thumb, vars.thumb_enable, false, vars.item_class);
			this.start_x 	= parseInt(vars.start_x);
			this.start_y 	= parseInt(vars.start_y);
			this.position 	= parseInt(vars.position);
		} catch(e) {
			if(debug) alert('Initialization of Category failed, missing or wrong parameter! ('+e+')');
		}
	} else {
		try {
			Item.call(this,
					'ctg',
					0,
					"category",
					$('#ctg_alias').val(),
					$('#ctg_alias').val()+'/',
					'',
					$('#ctg_alias').val(),
					'',
					false,
					false,
					'');
			this.start_x 	= 0;
			this.start_y 	= 0;
			this.position 	= 0;
		} catch (e) {
			if(debug) alert('Initialization of new empty Category failed, missing or wrong parameter! ('+e+')');
		}
	}
};
Category.prototype = new Item();
Category.prototype.start_x = 0;
Category.prototype.start_y = 0;
Category.prototype.position = 0;

/**
 * Loads the category's children.
 */
Category.prototype.loadChilds = function() {
	getProjects().load({prefix: getProjects().prefix, ctg_id: this.id}, null, false);
	getButtons().load({prefix: getButtons().prefix, ctg_id: this.id}, null, false);
};
/**
 * Checks if all the mandatory inputs for a updating/saving a category are correct. 
 */
Category.prototype.check = function() {
	getCategories().hideErrorMessages();
	var isValid = true;
	var ctg_alias = $('#ctg_alias').val() == '' ? false : true;
	var ctg_label = $('#ctg_label').val() == '' ? false : true;
	var ctg_thumb = $('#ctg_thumb_path').val() == '' ? false : true;
	var ctg_thumb_enable = $('#ctg_thumb_enable').is(':checked');
	if (!ctg_alias) {
		isValid = false;
		$('#'+this.prefix+'_alias').showError('Alias darf nicht leer sein!');
	}
	if (!ctg_label) {
		if (!ctg_thumb || !ctg_thumb_enable) {
			isValid = false;
			$('#'+this.prefix+'_label').showError('Ohne Thumbnail ist ein Label notwendig!');
		}
	}
	if (!ctg_thumb || !ctg_thumb_enable) {
		if (!ctg_label) {
			isValid = false;
			if (!ctg_thumb) {
				$('#'+this.prefix+'_thumb_container').showError('Kein Label erfordert ein Thumbnail!');
			} else {
				$('#'+this.prefix+'_thumb_enable').showError('Ohne Label muss Thumbnail aktiv sein!');
			}
		}
	}
	return isValid;
};